package com.dwxt.cdms.service;

import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.dao.OuFuDataBandSleepMapper;
import com.dwxt.cdms.dao.OuFuDataCardMapper;
import com.dwxt.cdms.entity.oufu.OuFuLocData;
import com.dwxt.common.base.BaseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Slf4j
public class OuFuQueryService {
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    @Autowired
    private StationService stationService;

    @Autowired
    private OuFuDataCardMapper cardMapper;

    @Autowired
    private OuFuDataBandSleepMapper bandSleepMapper;



    public BaseResult selectCardPage(Page page, String name, String type) {
        Integer loginUserStation = stationService.getLoginUserStation();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<OuFuLocData> cardList = cardMapper.selectCardPage(name, type,loginUserStation);
        return BaseResult.ok(new PageInfo(cardList));
    }

    /*public BaseResult selectBandSleepPage(Page page, String name) {
        return null;
    }

    private PageInfo getSleepData(String name) {
        List<Integer> customerIdList = bandSleepMapper.selectCustomerIdsBySleepData(name);
        PageInfo pageInfo = new PageInfo(customerIdList);
        List<NewSleepDataView> list = new ArrayList<>();
        for (Integer customerId : customerIdList) {
            NewSleepDataView newSleepDataView = getNewSleepDataView(customerId);
            if (newSleepDataView != null) {
                list.add(newSleepDataView);
            }
        }
        //按测量时间降序排序
        list.sort((m1, m2) -> m2.getEndTime().compareTo(m1.getEndTime()));
//        List<NewSleepDataView> list = sleepDataMapper.selectSleepDataViewByCondition(selectWatchDataVo);
        pageInfo.setList(list);
        return pageInfo;
    }

    public NewSleepDataView getNewSleepDataView(Integer customerId) {
        List<OuFuData> fuDataList = selectSleepByCustomerId(customerId);
        if (fuDataList.size() <= 0) {
            return null;
        }
        OuFuData ouFuData = fuDataList.get(0);
        int year = getYearMonthDay(ouFuData.getEndTime(), 1);
        int month = getYearMonthDay(ouFuData.getEndTime(), 2);
        int day = getYearMonthDay(ouFuData.getEndTime(), 3);
        String startTime;
        if (getYearMonthDay(ouFuData.getEndTime(), 4) > 12) {
            startTime = year + "/" + month + "/" + day + " 12:00:00";
        } else {
            if (day == 1) {
                Calendar cale = Calendar.getInstance();
                cale.set(Calendar.DAY_OF_MONTH, 0);
                int i = cale.get(Calendar.DAY_OF_MONTH);
                startTime = year + "/" + (month - 1) + "/" + i + " 12:00:00";
            } else {
                startTime = year + "/" + month + "/" + (day - 1) + " 12:00:00";
            }
        }
        List<OuFuData> ouFuDataList = selectSleepRangeRecord(customerId, startTime, ouFuData.getEndTime());
        if (ouFuDataList.size() <= 0) {
            return null;
        }
        return handle(ouFuDataList);
    }

    private List<OuFuData> handle(List<OuFuData> ouFuDataList, Integer customerId, List<NewSleepDataView> list) {
        //NewSleepDataView handle = null;
        String startTime;
        OuFuData ouFuData = ouFuDataList.get(0);
        String endTime = ouFuData.getEndTime();
        int year = getYearMonthDay(endTime, 1);
        int month = getYearMonthDay(endTime, 2);
        int day = getYearMonthDay(endTime, 3);
        if (getYearMonthDay(endTime, 4) > 12) {
            startTime = year + "/" + month + "/" + day + " 12:00:00";
        } else {
            startTime = year + "/" + month + "/" + (day - 1) + " 12:00:00";
        }
        List<OuFuData> rangList = selectSleepRangeRecord(customerId, startTime, endTime);
        NewSleepDataView sleepDataView = handle(rangList);
        list.add(sleepDataView);
        return ouFuDataList.stream().filter(ouFu -> !DateUtils.after(format, ouFu.getEndTime(), startTime)).collect(Collectors.toList());
    }

    private List<OuFuData> selectSleepRangeRecord(Integer customerId, String startTime, String endTime) {
        return OuFuDataBandSleepMapper.selectSleepRangeRecord(customerId, ReceiveDataType.SLEEP, startTime, endTime);
    }

    private List<OuFuData> selectSleepByCustomerId(Integer customerId) {
        OuFuDataExample example = new OuFuDataExample();
        example.setOrderByClause("create_time desc");
        example.createCriteria().andCustomerIdEqualTo(customerId).andTypeEqualTo(ReceiveDataType.SLEEP);
        List<OuFuData> ouFuDataList = OuFuDataBandSleepMapper.selectByExample(example);
        return ouFuDataList;
    }

    private int getYearMonthDay(String endTime, int type) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(endTime));
            if (type == 1) {
                return calendar.get(Calendar.YEAR);
            } else if (type == 2) {
                return calendar.get(Calendar.MONTH) + 1;
            } else if (type == 3) {
                return calendar.get(Calendar.DAY_OF_MONTH);
            } else if (type == 4) {
                return calendar.get(Calendar.HOUR_OF_DAY);
            } else {
                throw new RuntimeException("类型错误");
            }
        } catch (ParseException e) {
            throw new RuntimeException();
        }
    }*/


}

